package rpt.yto.flex.remote.data.dao.processor;

import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.concurrent.ConcurrentHashMap;

import rpt.yto.flex.remote.data.bean.CorpBean;
import rpt.yto.flex.remote.data.biz.QueryBiz;
import rpt.yto.flex.remote.data.dao.itf.IPubObject;
import rpt.yto.util.ConvertFunc;

@SuppressWarnings({"unchecked"})
public class Query_allpsn_mng {
	
	private Map<String , String> distinctionMap = null;
	private Map<String , String> mngDistrictMap = null;
	private Map<String , String> mngDistrictMap2 = null;
	private List<Map<String , Object>> pk_corpList = null;
	
 	private static Query_allpsn_mng querySqlFunc = new Query_allpsn_mng();
	
	private Query_allpsn_mng() {
		
	}
	
	public static final Query_allpsn_mng getInstance() {
		return querySqlFunc;
	}
	
	/**
	 *   全网按机构类型聚合表 - 全网人员类别
	 *   
	 * @param period
	 * @return
	 * @throws Exception
	 */
	public final StringBuilder getQueryTypeByCorp(String period , Integer rowno , String statistictype , String pk_corp , String type) throws Exception {
		
		StringBuilder builderTypeAll = new StringBuilder();
		
		builderTypeAll.append(" select "+rowno+" rowno , '"+statistictype+"' statistictype , sum(endcount) endcount , sum(endcount_w) endcount_w ,  ");
		builderTypeAll.append(" sum(comcount) comcount , sum(mngcount) mngcount , sum(procount) procount , sum(procount_acc) procount_acc ,  ");
		builderTypeAll.append(" sum(skillcount) skillcount , sum(skillcount_acc) skillcount_acc , sum(expcount) expcount , sum(expcount_acc) expcount_acc ,  ");
		builderTypeAll.append(" sum(roundcount) roundcount , sum(roundcount_acc) roundcount_acc , sum(cscount) cscount from ( ");
		// 期末从业人员数
		builderTypeAll.append(" select count(1) endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount "); 
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		
		builderTypeAll.append(" union all  ");
		// 期末女性从业人员数
		builderTypeAll.append(" select 0 endcount , count(1) endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and bd_psnbasdoc.sex = '女' ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 主管以上管理人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , count(1) comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '%主管' or om_job.jobname like '%经理' or om_job.jobname like '%总监' or om_job.jobname like '%总裁' or om_job.jobname like '%高级专员'  or  om_job.jobname like '%专家') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 经理级以上人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , count(1) mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '%经理' or om_job.jobname like '%总监' or om_job.jobname like '%总裁'  or  om_job.jobname like '%专家') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 专业技术人员数
		// procount
		// 取得技术职称人员数
		// procount_acc
		// 技能人员数 
		// skillcount 
		// 取得技能资格证人员数
		// skillcount_acc
		// 快递业务人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , count(1) expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '%业务员') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 取得业务员资格证人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , count(1) expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '%业务员') and nvl(bd_psndoc.groupdef1 , 'N') = 'Y' ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 分拣操作人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , count(1) roundcount , 0 roundcount_acc , 0 cscount ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '操作%') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 取得操作员资格证人员
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , count(1) roundcount_acc , 0 cscount ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		builderTypeAll.append(" and (om_job.jobname like '操作%') and nvl(bd_psndoc.groupdef1 , 'N') = 'Y' ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" union all  ");
		// 客服人员数
		builderTypeAll.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
		builderTypeAll.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , count(1) cscount ");
		builderTypeAll.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
		builderTypeAll.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
		builderTypeAll.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
		builderTypeAll.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
		builderTypeAll.append("  and ((bd_psndoc.psnclscope = 0 ");
		builderTypeAll.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
		builderTypeAll.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
		if(type != null && type.equals(IPubObject.TYPE[0])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		if(type != null && type.equals(IPubObject.TYPE[1])) builderTypeAll.append(getCorpJoinString(pk_corp , type));
		builderTypeAll.append(" and (om_job.jobname like '%客%') ) ");
		
		return builderTypeAll;
		
	}
	
	/**
	 * 
	 * 全网按机构类型聚合表 - distinctionMap中的类别
	 * 
	 * @param period
	 * @return
	 * @throws Exception
	 */
	public final StringBuilder getQueryTypeByMap(String period) throws Exception {
		
		if(distinctionMap == null)
			initDistinctionMap();
		
		StringBuilder builderTypeAppend2 = new StringBuilder();
		
		Iterator iter = distinctionMap.entrySet().iterator();
		int rowno = 2;
		while(iter.hasNext()) {
			
			Entry entry = (Entry) iter.next();
			String distinction = (String) entry.getKey();
			String statistictype = (String) entry.getValue();
			
			StringBuilder builderTypeByMap = new StringBuilder();
			
			builderTypeByMap.append(" select "+rowno+" rowno , '"+statistictype+"' statistictype , sum(endcount) endcount , sum(endcount_w) endcount_w ,  ");
			builderTypeByMap.append(" sum(comcount) comcount , sum(mngcount) mngcount , sum(procount) procount , sum(procount_acc) procount_acc ,  ");
			builderTypeByMap.append(" sum(skillcount) skillcount , sum(skillcount_acc) skillcount_acc , sum(expcount) expcount , sum(expcount_acc) expcount_acc ,  ");
			builderTypeByMap.append(" sum(roundcount) roundcount , sum(roundcount_acc) roundcount_acc , sum(cscount) cscount from ( ");
			// 期末从业人员数
			builderTypeByMap.append(" select count(1) endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount "); 
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 期末女性从业人员数
			builderTypeByMap.append(" select 0 endcount , count(1) endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and bd_psnbasdoc.sex = '女' ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 主管以上管理人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , count(1) comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%主管' or om_job.jobname like '%经理' or om_job.jobname like '%总监' or om_job.jobname like '%总裁' or om_job.jobname like '%高级专员'  or  om_job.jobname like '%专家') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 经理级以上人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , count(1) mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%经理' or om_job.jobname like '%总监' or om_job.jobname like '%总裁'  or  om_job.jobname like '%专家') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 专业技术人员数
			// procount
			// 取得技术职称人员数
			// procount_acc
			// 技能人员数 
			// skillcount 
			// 取得技能资格证人员数
			// skillcount_acc
			// 快递业务人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , count(1) expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%业务员') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 取得业务员资格证人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , count(1) expcount_acc , 0 roundcount , 0 roundcount_acc , 0 cscount  ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '%业务员') and nvl(bd_psndoc.groupdef1 , 'N') = 'Y' ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 分拣操作人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , count(1) roundcount , 0 roundcount_acc , 0 cscount ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '操作%') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 取得操作员资格证人员
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , count(1) roundcount_acc , 0 cscount ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and (om_job.jobname like '操作%') and nvl(bd_psndoc.groupdef1 , 'N') = 'Y' ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" union all  ");
			// 客服人员数
			builderTypeByMap.append(" select 0 endcount , 0 endcount_w , 0 comcount , 0 mngcount , 0 procount , 0 procount_acc ,  ");
			builderTypeByMap.append(" 0 skillcount , 0 skillcount_acc , 0 expcount , 0 expcount_acc , 0 roundcount , 0 roundcount_acc , count(1) cscount ");
			builderTypeByMap.append(" from bd_psndoc  , bd_psnbasdoc , om_job , bd_corp  ");
			builderTypeByMap.append(" where bd_psndoc.pk_psnbasdoc = bd_psnbasdoc.pk_psnbasdoc(+) ");
			builderTypeByMap.append(" and om_job.pk_om_job(+) = bd_psndoc.pk_om_job ");
			builderTypeByMap.append(" and bd_corp.pk_corp(+) = bd_psndoc.pk_corp ");
			builderTypeByMap.append("  and ((bd_psndoc.psnclscope = 0 ");
			builderTypeByMap.append("  and bd_psndoc.outdutydate is null) or bd_psndoc.outdutydate > '"+period+"~') and bd_psndoc.indutydate <= '"+period+"~' ");
			builderTypeByMap.append(" and (nvl(om_job.isabort , 'N') = 'N' or om_job.abortdate like '"+period+"%') and (bd_corp.isseal = 'N' or bd_corp.sealeddate >= '"+period+"~') ");
			builderTypeByMap.append(" and bd_corp.isworkingunit = 'Y' ");
			builderTypeByMap.append(" and bd_corp.unitdistinction = '"+distinction+"' ");
			builderTypeByMap.append(" and (om_job.jobname like '%客%') ) ");
			
			builderTypeAppend2.append(builderTypeByMap).append(" union all ");
			
			rowno ++;
			
		}
		
		builderTypeAppend2 = new StringBuilder(builderTypeAppend2.substring(0 , builderTypeAppend2.length() - " union all ".length()));
		
		return builderTypeAppend2;
	}
	
	
	public final Map<String , String> initDistinctionMap() throws Exception {
		distinctionMap = new ConcurrentHashMap<String , String>();
		distinctionMap.put("ORGCORP00000000A0015", "总公司人数");
		
		return distinctionMap;
	}
	
	public final Map<String , String> getMngDistict() throws Exception {
		
		mngDistrictMap = new ConcurrentHashMap<String , String>();
		StringBuilder builderCorp = new StringBuilder();
		builderCorp.append(" select pk_corp , '按' || unitname || '聚合人数' unitname from bd_corp where unitname like '%管理区%' and unitname <> '山东管理区市场部' " );
		
		List<Map<String , Object>> corpList = (List<Map<String , Object>>) QueryBiz.getInstance().executeQuery(builderCorp.toString(), CorpBean.class);
		
		Integer rowno = distinctionMap.size() + 2;
		
		for(Map<String , Object> corpVO : corpList) {
			mngDistrictMap.put(String.valueOf(corpVO.get("pk_corp")), rowno + "~" + corpVO.get("unitname"));
			
			rowno ++;
		}
		
		return mngDistrictMap;
	}
	
	public final Map<String , String> getMngDistict2() throws Exception {
		
		mngDistrictMap2 = new ConcurrentHashMap<String , String>();
		StringBuilder builderCorp = new StringBuilder();
		builderCorp.append(" select to_char(wmsys.wm_concat(chr(39) || bd_corp.pk_corp || chr(39))) pk_corp , '按' || bd_defdoc.docname || '聚合人数' unitname from bd_corp left join bd_defdoc on bd_corp.region = bd_defdoc.pk_defdoc where docname like '广西%' or docname like '海南%' or docname like '福建%' group by bd_defdoc.docname " );
		
		List<Map<String , Object>> corpList = (List<Map<String , Object>>) QueryBiz.getInstance().executeQuery(builderCorp.toString(), CorpBean.class);
		
		Integer rowno = mngDistrictMap.size() + distinctionMap.size() + 2;
		
		for(Map<String , Object> corpVO : corpList) {
			mngDistrictMap2.put(String.valueOf(corpVO.get("pk_corp")), rowno + "~" + corpVO.get("unitname"));
			
			rowno ++;
		}
		
		return mngDistrictMap2;
	}
	
	
	private final String getCorpJoinString(String pk_corp , String type) throws Exception {
		
		if(type.equals(IPubObject.TYPE[0])) {
			
			if(pk_corpList == null) {
				StringBuilder builderCorp = new StringBuilder();
				builderCorp.append(" select to_char(wmsys.wm_concat(chr(39) || bd_corp.pk_corp || chr(39))) pk_corp from bd_corp left join bd_defdoc on bd_corp.region = bd_defdoc.pk_defdoc where docname like '广西%' or docname like '海南%' or docname like '福建%' " );
				
				pk_corpList = (List<Map<String , Object>>) QueryBiz.getInstance().executeQuery(builderCorp.toString(), CorpBean.class);
			}
			
			return " and bd_corp.pk_corp in (select pk_corp from bd_corp where pk_corp = '"+pk_corp+"' union all  select pk_corp from bd_corp where fathercorp = '"+pk_corp+"' union all select pk_corp from bd_corp where fathercorp in (select pk_corp from bd_corp where fathercorp = '"+pk_corp+"')) and bd_corp.pk_corp not in ("+pk_corpList.get(0).get("pk_corp")+") ";
		}
		
		if(type.equals(IPubObject.TYPE[1])) return " and bd_corp.pk_corp in ( "+ pk_corp +" ) ";
		
		return null;
	}
	
	public final String getQuerySQL(String period) throws Exception {
		
		StringBuilder builderResult = Query_allpsn_mng.getInstance().getQueryTypeByCorp(period, 1, "全网合计", null, null)
				.append(" union all ").append(Query_allpsn_mng.getInstance().getQueryTypeByMap(period));
		
		Iterator iter = Query_allpsn_mng.getInstance().getMngDistict().entrySet().iterator();
		
		while(iter.hasNext()) {
			Entry entry = (Entry) iter.next();
			
			String pk_corp = ConvertFunc.getInstance().change(entry.getKey());
			String[] vals = ConvertFunc.getInstance().change(entry.getValue()).split("~");
			
			Integer rowno = Integer.valueOf(vals[0]);
			String statistictype = vals[1];
			
			builderResult.append("union all ").append(Query_allpsn_mng.getInstance().getQueryTypeByCorp(period, rowno, statistictype, pk_corp, IPubObject.TYPE[0]));
			
		}
		
		iter = Query_allpsn_mng.getInstance().getMngDistict2().entrySet().iterator();
		while(iter.hasNext()) {
			Entry entry = (Entry) iter.next();
			
			String pk_corp = ConvertFunc.getInstance().change(entry.getKey());
			String[] vals = ConvertFunc.getInstance().change(entry.getValue()).split("~");
			
			Integer rowno = Integer.valueOf(vals[0]);
			String statistictype = vals[1];
			
			builderResult.append("union all ").append(Query_allpsn_mng.getInstance().getQueryTypeByCorp(period, rowno, statistictype, pk_corp, IPubObject.TYPE[1]));
			
		}
		
		return builderResult.toString();
		
	}

}
